package com.tennet.dao;

import java.io.Serializable;
import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

/**
 * Dao基本模版类，供子类复用。不允许实例化
 * @author   pugang
 * @Date	 2011	2011-4-12		下午10:34:38
 */
public class BaseDao<T> extends SqlSessionDaoSupport {
	public static final String POSTFIX_INSERT = "insert";

	public static final String POSTFIX_UPDATE = "update";

	public static final String POSTFIX_DELETE_PRIAMARYKEY = "deleteByPrimaryKey";

	public static final String POSTFIX_DELETE_PARAMETER = "deleteByParameter";

	public static final String POSTFIX_SELECT_PRIAMARYKEY = "selectByPrimaryKey";

	public static final String POSTFIX_SELECT_PARAMETER = "selectByParameter";

	public static final String POSTFIX_SELECTSQL = "selectBySql";

	public static final String POSTFIX_COUNT = "count";

	public static final String CONTACT_CHAR = ".";

	/**
	 * 
	 * delete:(删除一个实体)
	 * 2011-4-12      
	 * pugang
	 * @param  parameter  参数，推荐使用Bean或Map作为参数载体 
	 * @return void    
	 * @throws
	 */
	public void delete(Object parameter) {
		getSqlSession().insert(getStatementId(POSTFIX_DELETE_PRIAMARYKEY), parameter);
	}

	/**
	 * 
	 * delete:(根据id删除，此方法是为了减少用户编码)
	 * 2011-4-12      
	 * pugang
	 * @param  id  主键值 
	 * @return void    
	 * @throws
	 */
	public void delete(Serializable id) {
		getSqlSession().insert(getStatementId(POSTFIX_DELETE_PRIAMARYKEY), id);
	}

	@SuppressWarnings("unchecked")
	public List<T> find(Object parameter) {
		return (List<T>) getSqlSession().selectList(getStatementId(POSTFIX_SELECT_PARAMETER), parameter);
	}

	/**
	 * 
	 * find:(根据主键查找)
	 * 2011-4-12      
	 * pugang
	 * @param  id
	 * @param  @return   
	 * @return T    
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public T find(Serializable id) {
		return (T) getSqlSession().selectOne(getStatementId(POSTFIX_SELECT_PRIAMARYKEY), id);
	}

	public int getCount() {
		return (Integer) getSqlSession().selectOne(getStatementId(POSTFIX_COUNT));
	}

	private String getNameSpace() {
		String nameSpace = null;
		nameSpace = this.getClass().getCanonicalName().replace("Dao", "");
		return nameSpace;
	}

	/**
	 * get statement id in SQL Map file
	 * 
	 * @param NameSpace
	 * @param suffix
	 * @return
	 */
	private String getStatementId(String suffix) {
		return getNameSpace() + CONTACT_CHAR + suffix;
	}

	public void insert(List<T> list) {
		//TODO 事务处理还不确定
		for (T t : list) {
			getSqlSession().insert(getStatementId(POSTFIX_INSERT), t);
		}
	}

	/**
	 * 新增对象到数据库
	 * 
	 * @param o
	 *            对象实体
	 */
	public void insert(T o) {
		getSqlSession().insert(getStatementId(POSTFIX_INSERT), o);
	}

	/**
	 * 更新对象实体到数据库
	 * 
	 * @param o
	 *            对象实体
	 */
	public void update(T o) {
		getSqlSession().update(getStatementId(POSTFIX_UPDATE), o);
	}

	public void update(List<T> list) {
		//TODO 事务处理还不确定
		for (T t : list) {
			getSqlSession().update(getStatementId(POSTFIX_UPDATE), t);
		}
	}
}
